package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.gen.entity.HInventory;
import com.zhongwang.mms.gen.entity.HInventoryProjectMark;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.dao.HInventoryProjectMarkDao;
import com.zhongwang.mms.module.half.model.HInventoryProjectMarkModel;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @description:库存划项目执行
 * @author: th
 * @time: 2020/5/9 15:22
 */
@Service
@Log4j2
public class InvToProExcService {

    @Autowired
    private HInventoryDao hInventoryDao;
    @Autowired
    private HInventoryProjectMarkDao hInventoryProjectMarkDao;
    @Autowired
    private LogService logService;

    public List<HInventoryProjectMarkModel> selectHInventoryProjectMarkByQuery(HInventoryProjectMarkModel query) {

        query.setStatus(2);
        return hInventoryProjectMarkDao.selectHInventoryProjectMarkByQuery(query);
    }

    /**
     * 库存划项目执行
     * @param hInventoryProjectMarkModel
     * @return
     */
    @Transactional
    public boolean execApply(HInventoryProjectMarkModel hInventoryProjectMarkModel) {

        try {
            //更改库存
            //首先获取项目划库存实体类
            List<HInventoryProjectMark> hInventoryProjectMarks = hInventoryProjectMarkDao.getHInvProMarkByIpmIds(hInventoryProjectMarkModel.getIpmIds());
            for (int i = 0; i < hInventoryProjectMarks.size(); i++) {
                HInventoryProjectMark hInventoryProject = hInventoryProjectMarks.get(i);
                HInventory hInventory = hInventoryDao.selectAllById(hInventoryProject.getIpmBeforeItyId());
                //插入串过来库存的数据
                hInventory.setItyProCode(hInventoryProject.getIpmInProjectCode());
                hInventory.setItyProName(hInventoryProject.getIpmInProjectName());
                hInventory.setItyNum(hInventoryProject.getIpmNum());
                hInventory.setItyAvaNum(hInventoryProject.getIpmNum());
                hInventory.setItyWeight(hInventoryProject.getIpmWeight());
                hInventory.setItyAvaWeight(hInventoryProject.getIpmWeight());
                hInventory.setItyStockType(2);
                hInventoryDao.insert(hInventory);
                //反写使用的库存信息到串料数据中,等待提报计划指定。
                hInventoryProject.setIpmAfterItyId(hInventory.getItyId());
                hInventoryProject.setIpmAppStatus(3);
                hInventoryProject.setIpmExecutorId(ShiroUtils.getUser().getId());
                hInventoryProject.setIpmExecDate(new Date());
                hInventoryProjectMarkDao.updateByPrimaryKeySelective(hInventoryProject);
                //获取原库存数据并更改其值为,库存数量为哭出声来减去可用数量，可用数量清零。
                hInventory = hInventoryDao.selectAllById(hInventoryProject.getIpmBeforeItyId());
                hInventory.setItyNum(hInventory.getItyNum() -hInventoryProject.getIpmNum());
                if(hInventoryProject.getIpmWeight() != null && hInventoryProject.getIpmWeight() != 0){
                    hInventory.setItyWeight(hInventory.getItyWeight() - hInventoryProject.getIpmWeight());
                }
                hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 库存划项目撤销执行
     * @param hInventoryProjectMarkModel
     * @return
     */
    @Transactional
    public boolean revokeApply(HInventoryProjectMarkModel hInventoryProjectMarkModel) {

        try {
            List<HInventoryProjectMark> hInventoryProjectMarks = hInventoryProjectMarkDao.getHInvProMarkByIpmIds(hInventoryProjectMarkModel.getIpmIds());
            //检测所有申请是否已经出库或被串料，如何验证出库，即串料之后的新库存数据可用数量或者库存数量小于串料数量则不可撤销
            for (HInventoryProjectMark hInventoryProjectMark : hInventoryProjectMarks) {

                //获取因项目划库存新生成的数据
                HInventory hInventory = hInventoryDao.selectAllById(hInventoryProjectMark.getIpmAfterItyId());
                //如果被执行项目化库存则不可撤销
                if (hInventory.getItyStockType().equals(1)) {
                    return false;
                }
                //如果发现因出库、标记造成的库存数量或可用数量无法撤销则返回假
                if (hInventory.getItyNum() < hInventoryProjectMark.getIpmNum() ||
                        hInventory.getItyAvaNum() < hInventoryProjectMark.getIpmNum()) {
                    return false;
                }
                //如果被指定，无法撤销
                if(hInventoryProjectMark.getIpmRppId() != null){
                    return false;
                }
            }
            //更改库存
            //首先获取库存划项目实体类
            for (int i = 0; i < hInventoryProjectMarks.size(); i++) {
                HInventoryProjectMark hInventoryProjectMark = hInventoryProjectMarks.get(i);

                //获取库存划项目生成的库存，清零0库存
                HInventory hInventory = hInventoryDao.selectAllById(hInventoryProjectMark.getIpmAfterItyId());
                hInventory.setItyNum(0d);
                hInventory.setItyAvaNum(0d);
                hInventory.setItyWeight(0d);
                hInventory.setItyAvaWeight(0d);
                hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory);
                //获取被划的库存，回写增加被串料的数量
                HInventory hInventory1 = hInventoryDao.selectAllById(hInventoryProjectMark.getIpmBeforeItyId());
                hInventory1.setItyNum(hInventory1.getItyNum() + hInventoryProjectMark.getIpmNum());
                if(hInventoryProjectMark.getIpmWeight() != null && hInventoryProjectMark.getIpmWeight() != 0){
                    hInventory1.setItyWeight(hInventory1.getItyWeight() + hInventoryProjectMark.getIpmWeight());
                }
                //hInventory1.setItyAvaNum(hInventory1.getItyAvaNum() + hInventoryProjectMark.getIpmNum());
                hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory1);
                //作废此单：清空标记计划ID为0，申请状态为被驳回。
                hInventoryProjectMark.setIpmAppStatus(2);
                hInventoryProjectMarkDao.updateByPrimaryKeySelective(hInventoryProjectMark);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 驳回
     * @param param
     * @return
     */
    @Transactional
    public boolean invalid(HInventoryProjectMarkModel param) {
        try {
            List<HInventoryProjectMark> list = hInventoryProjectMarkDao.getHInvProMarkByIpmIds(param.getIpmIds());
            for (HInventoryProjectMark hInventoryProjectMark:list) {
                HInventory hInventory = hInventoryDao.selectAllById(hInventoryProjectMark.getIpmBeforeItyId());
                logService.saveLog("【成品库】驳回库存划项目标记","驳回的库存划项目的库存物料描述："+hInventory.getItyMatName());
            }
            param.setIpmAppStatus(4);
            hInventoryProjectMarkDao.updateHInvProMarkAppyStatusByIpmIds(param);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return false;
        }
        return true;
    }
}
